<?PHP

// +--------------------------------------------------------------------+
// | Based on "inlineRSS" by Cal Demaine -- http://www.iconophobia.com/ |
// +--------------------------------------------------------------------+

Markup('PMinlineRSS','<fulltext','/\\(:inlineRSS (.*?):\\)/e', 'PMInlineRSS("$1")');

function PMInlineRSS($opts) {

  $cookbookpath = 'cookbook/PMinlineRSS/'; // Path to directory containing XSLT files
  $cachepath = 'pub/';                     // Path to directory for cache files
  $fileprefix = 'cache_';                  // What cache file names get prefixed with
  $xsltfile = 'PMinlineRSSt2t.xslt';          // The default xslt file
  $maxitems = '8';
  
  $args = ParseArgs($opts);

  $p_feed = $args['feed'];
  $p_cachetime = 0;
  $p_cachetime = $args['cachetime'];
  $p_cachename = $args['cachename'];
  $p_altXSLT = $args['altXSLT'];
  $p_maxitems  = $args['maxitems'];
  $cached = FALSE;
  $xml="";
        
  if ($p_cachetime == 0) { $p_cachetime = 30; }    

  // If a caching file is specified, see if we should just use it
  if (!$p_cachename == "") {
    $cachepathname = $cachepath . $fileprefix . $p_cachename . ".xml";
    if (file_exists($cachepathname)) {
      $age = time() - filectime($cachepathname);
      if ($age <= ($p_cachetime * 60)) {
        $xml = file_get_contents($cachepathname);
        $cached=TRUE;
      }
    }
  }
    
  // We got nothing from cache (or it didn't exist or wasn't specified)
  if ($xml == "") {
    // Use CURL because, well, it's a tiny bit safer.
      
    if (function_exists('curl_init') and stristr($p_feed,"http:") ) {
      $curl_handle=curl_init();
      curl_setopt($curl_handle,CURLOPT_URL,$p_feed);
      curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10);
      curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
      $curlxml = curl_exec($curl_handle);
      curl_close($curl_handle);

      if (empty($curlxml))  {
        die("Error reading feed $p_feed using curl.");
        $writefile = FALSE;
      } else {
        $xml = $curlxml;
        $writefile = TRUE;
      }
            
    } else {
      //  If CURL is giving you problems, use the line below instead.
      $filegetxml = file_get_contents($p_feed);
      if (empty($filegetxml)) {
        die("Error reading feed $p_feed using file_get.");
        $writefile = FALSE;
      } else {
        $xml = $filegetxml;
        $writefile = TRUE;
      }
    } 
        
    if ($writefile && (!$p_cachename == "")) {
      if (!($handle = fopen($cachepathname,'w'))) die ("Error opening $cachepathname - possible permissions issue - directory permissions are " . substr(sprintf('%o', fileperms($cookbookpath)), -4));
      fwrite($handle,$xml);
      fclose($handle);
    }
  }
    
  if (strlen($xml) < 1) die ("XML feed $cachename is empty.\n");
    
  if (!empty($p_altXSLT)) { $xsltfile = $p_altXSLT; } // Use the alternate XSLT file if specified
              
  $xsl = file_get_contents($cookbookpath . $xsltfile) or die("Error reading XSL file " . $xsltfile);
        
  // This is a switchboard to choose the proper XSLT processing engine and grind through it.
        
  if (PHP_VERSION >= 5) {
    $xslt = new xsltProcessor;
    $xslt->importStyleSheet(DomDocument::loadXML($xsl));
    $xslt_result = $xslt->transformToXML(DomDocument::loadXML($xml));   
           
  } elseif (function_exists('domxml_open_mem') && function_exists('domxml_xslt_stylesheet')) {  // PHP 4 DOM_XML support
    if (!$domXml = domxml_open_mem($xml)) {
      $result = "Error while parsing the xml document\n";
    }
    
    $domXsltObj = domxml_xslt_stylesheet( $xsl );
    $domTranObj = $domXsltObj->process( $domXml );
    $xslt_result = $domXsltObj->result_dump_mem( $domTranObj );
    
  } elseif (function_exists('xslt_create')) {  // PHP 4 XSLT library
    $arguments = array (
      '/_xml' => $xml,
      '/_xsl' => $xsl
    );
    
    $xslt_inst = xslt_create();    
    $xslt_result = xslt_process($xslt_inst,'arg:/_xml','arg:/_xsl', NULL, $arguments);
    xslt_free($xslt_inst);
          
  } else {  // Nothing, no valid processor found.  Curses.
    die("No valid XSLT processor found");     
  }
        
  if (empty($xslt_result)) {
    $xslt_result = "Horrific XSLT error - $casualname returned empty - verify $cachepathname is a valid XML file and check logs.\n";
  }   
      
  if ( $p_maxitems && is_numeric($p_maxitems) && $p_maxitems > 0 && $p_maxitems < 1000 ) {
      $temp = preg_split( "#\r?\n#", $xslt_result );
      $result = implode( "\n", array_slice( $temp, 0, $p_maxitems+1 ) );
  }
  else {
      $temp = preg_split( "#\r?\n#", $xslt_result );
      $result = implode( "\n", array_slice( $temp, 0, $maxitems+1 ) );
      // $result = $xslt_result;
  }
  
  
return $result;  
}

?>
